package com.zhiqianbao.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;

import com.zhiqianbao.R;
import com.zhiqianbao.utils.LogUtils;
import com.zhiqianbao.utils.WorthPurseUtil;
import com.tencent.tauth.Tencent;

import java.io.ByteArrayOutputStream;

/**
 * Created by hp on 2017/11/22.
 */
public class TestActivity extends AppCompatActivity {
    WebView webView;
    Tencent _tencent;
    Button bt_test;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_layout);
        bt_test=(Button)findViewById(R.id.bt_test);
        _tencent = Tencent.createInstance("1105139983", this);
        webView=(WebView)findViewById(R.id.web_view);
        //webView.loadUrl("file:///android_asset/weichat.jpg");
        //图片较大强制开启GPU渲染
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        // 设置可以支持缩放
        webView.getSettings().setSupportZoom(true);
        // 设置出现缩放工具
        webView.getSettings().setBuiltInZoomControls(false);
        //扩大比例的缩放
        webView.getSettings().setUseWideViewPort(true);
        //自适应屏幕
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setLoadWithOverviewMode(true);
        bt_test.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  WorthPurseUtil.loginWorthPurse(TestActivity.this,"100100");
//                WXWebpageObject webpage = new WXWebpageObject();
//                webpage.webpageUrl = "http://www.qq.com";
//                WXMediaMessage msg = new WXMediaMessage(webpage);
//                msg.title = "WebPage Title ";
//                msg.description = "WebPage Description ";
//                Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.mimidai_start);
//                Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, 100, 100, true);
//                bmp.recycle();
//                msg.thumbData = bitmap2Bytes(thumbBmp);
//                SendMessageToWX.Req req = new SendMessageToWX.Req();
//                req.transaction = buildTransaction("webpage");
//                req.message = msg;
//                req.scene = SendMessageToWX.Req.WXSceneSession;
//                MyApplication.mWxApi.sendReq(req);


//                final Bundle bundle = new Bundle();
//                bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
//                bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "http://www.baidu.com");
//                bundle.putString(QQShare.SHARE_TO_QQ_TITLE, "qq分享");
//                bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, "qq分享测试时生活会");
//                bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "http://image.xmcdn.com/group31/M0B/32/99/wKgJSVmsrCriVU1JAAGXUWKbBUM350_mobile_large.jpg?op_type=5&upload_type=album&device_type=ios&name=mobile_large&magick=jpg&strip=1&quality=6");
//                bundle.putString(QQShare.SHARE_TO_QQ_APP_NAME, "米米贷");
//                _tencent.shareToQQ(TestActivity.this, bundle, new IUiListener() {
//                    @Override
//                    public void onComplete(Object o) {
//
//                    }
//
//                    @Override
//                    public void onError(UiError uiError) {
//
//                    }
//
//                    @Override
//                    public void onCancel() {
//
//                    }
//                });
            }
        });
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }
    public static final int IMAGE_SIZE=32768;//微信分享图片大小限制
    /**
     * Bitmap转换成byte[]并且进行压缩,压缩到不大于maxkb
     * @param bitmap
     * @return
     */
    public static byte[] bitmap2Bytes(Bitmap bitmap) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
        int options = 100;
        int counts=0;
        while (output.toByteArray().length > IMAGE_SIZE && options != 10) {
            output.reset(); //清空output
            bitmap.compress(Bitmap.CompressFormat.JPEG, options, output);//这里压缩options%，把压缩后的数据存放到output中
            options -= 10;
            counts++;
            LogUtils.d("bts..........",""+counts);
        }
        byte[] bts=output.toByteArray();
        LogUtils.d("bts..........",""+bts.length);
        return output.toByteArray();
    }
}
